// pages/index/index_order_quan.js
var App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    discountList: [],
    useDiscount:false, //使用该优惠
    choseIndex: '-1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let list = JSON.parse(options.list);
    list.forEach((item)=>{
      item.coupon_ident = JSON.parse(item.coupon_ident)
    })
    this.setData({
      discountList: list,
    })
    
    let cIndex = wx.getStorageSync('discountIndex');
    if (cIndex != '') {
      if (cIndex == -1) {
        this.setData({
          useDiscount: false,
          choseIndex: cIndex
        })
      } else {
        let list = this.data.discountList;
        list[cIndex].chose = true;
        this.setData({
          useDiscount: true,
          discountList: list,
          choseIndex: cIndex
        })
      }

    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 优惠券详情页
  showDetailMsg: function (e) {
    var item = e.currentTarget.dataset.index;
    let arr = 'discountList[' + item + '].show';
    this.setData({
      [arr]: !this.data.discountList[item].show
    })
  },

  // 不使用优惠券
  notUseDiscount: function () {
    let list = this.data.discountList;
    for (let i = 0; i < list.length; i++) {
      list[i].chose = false;
    }
    this.setData({
      useDiscount: false,
      discountList: list,
      choseIndex: '-1'//不使用优惠券标记为-1
    })
  },
  onChangeDiscount: function (e) {
    let index = e.currentTarget.dataset.index;
    let list = this.data.discountList;

    for (let i = 0; i < list.length; i++) {
      list[i].chose = false;
    }
    list[index].chose = true;
    this.setData({
      useDiscount: true,
      discountList: list,
      choseIndex: String(index)
    })
  },
  // 确认按钮
  onCommitHandle: function () {
    wx.setStorageSync('discountIndex', this.data.choseIndex);
    wx.navigateBack({
      delta: '1'
    })
  }

})